package cn.lanqiao.ui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import cn.lanqiao.entity.Course;
import java.awt.Font;

public class TeacherCourse extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TeacherCourse frame = new TeacherCourse();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TeacherCourse() {
		setClosable(true);
		setTitle("学生选课信息");
		setBounds(100, 100, 703, 390);
		this.setLocation(150, 50);
		getContentPane().setLayout(null);
		JScrollPane scrollPane=new JScrollPane();
		scrollPane.setBounds(0, 0, 703, 186);
		getContentPane().add(scrollPane);
		table = new JTable();
		//设置表格数据居中
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
		cr.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, cr);
		//设置行高
		table.setRowHeight(30);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
	   bindToJTable();
		scrollPane.setViewportView(table);
		
		JButton btnNewButton = new JButton("添加课程");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//弹出添加课程框
				TeacherAddCourseFrm frm=new TeacherAddCourseFrm(TeacherCourse.this);
				frm.setVisible(true);
				
			}
		});
		btnNewButton.setBounds(51, 280, 125, 46);
		getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("修改课程");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//判断用户是否选择要修改的联系人
				int count=table.getSelectedRowCount();
				if(count==0) {
					JOptionPane.showMessageDialog(null, "请选择要修改的联系人");
					return;
				}
				
				//拿要修改的联系人的信息
				String cid,cname,cteacher,cexam;
				int rowIndex=table.getSelectedRow();
				
				cid=table.getValueAt(rowIndex,0).toString();
				cname=table.getValueAt(rowIndex,1).toString();
				cteacher=table.getValueAt(rowIndex,2).toString();
				cexam=table.getValueAt(rowIndex,3).toString();
				//窗体传递数据
				Course course=new Course(cid,cname,cteacher,cexam);
				
               //弹出对话框
				TeacherUpdateCourseFrm frm=new TeacherUpdateCourseFrm(course,table);
				frm.setVisible(true);
//				table.setModel(new DefaultTableModel(
//						service.list(),
//						new String[] {
//								"\u7F16\u53F7", "\u5B66\u53F7", "\u6210\u7EE9","\u8003\u8BD5"
//						}
//					));
			}
		});
		btnNewButton_1.setBounds(279, 280, 125, 46);
		getContentPane().add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("删除课程");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//删除确认
				int val = JOptionPane.showConfirmDialog(null, "确认删除吗？", "删除确认", JOptionPane.YES_OPTION);
				if(val==1) {
					return ;
				}
				//实现删除
				int rowIndex=table.getSelectedRow();
				//获取单元格数据
				String id=table.getValueAt(rowIndex, 0).toString();
				//实现删除
				cn.lanqiao.service.CourseService service=new cn.lanqiao.service.impl.CourseServiceImpl();
				service.deleteCourseByid(id);
				//刷新窗体中的table数据
				
				table.setModel(new DefaultTableModel(
						service.list(),
						new String[] {
								"\u7F16\u53F7", "\u5B66\u53F7", "\u6210\u7EE9","\u8003\u8BD5"
						}
					));
			}
		});
		btnNewButton_2.setBounds(496, 272, 118, 49);
		getContentPane().add(btnNewButton_2);
			}

	public void bindToJTable() {
		cn.lanqiao.service.CourseService service=new cn.lanqiao.service.impl.CourseServiceImpl();
		   Object[][] datas = service.list();
			String[] strs = new String[] {"\u7F16\u53F7", "\u5B66\u53F7", "\u6210\u7EE9","\u8003\u8BD5"};
			table.setModel(new DefaultTableModel(datas,strs));
	}
}